package com.server.view;

import com.common.User;
import com.server.controller.RegisteProcess;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

/***注册页面：
 * @author: lyh
 * @date: 2021/9/21
 * @update 2021/9/28
 * 该类是注册页面，负责新用户的注册
 */
public class RegisterFrame extends JFrame {
    private static final long serialVersionUID = -768631070458723803L;
    private JPasswordField jpf1;    //密码框
    private JPasswordField jpf2;    //确定密码框
    private JTextField jtf;         //昵称框
    private JTextField jtf2;        //真实姓名框
    private JComboBox head;
    private JRadioButton sex0;
    private JRadioButton sex1;
    private JButton ok;
    private JButton reset;
    private JButton cancel;

    public RegisterFrame(){
        try {
            org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
        }catch (Exception e){
            System.out.println("炫彩效果显示失败");
        }
        this.init();
        setVisible(true);
    }

    public void init(){
        this.setTitle("注册页面");
        //使界面在屏幕中央
        setBounds(((Toolkit.getDefaultToolkit().getScreenSize().width)/2)-300,
                ((Toolkit.getDefaultToolkit().getScreenSize().height)/2)-300,
                500,400);

        //暂时不设置布局
        getContentPane().setLayout(null);
        //不给用户调整界面大小和位置
        setResizable(false);

        JLabel label = new JLabel("昵称*:"); //label显示
        label.setBounds(12, 36, 59, 17);
        getContentPane().add(label);

        JLabel label2=new JLabel("真实姓名:");
        label2.setBounds(12,145,100,20);
        getContentPane().add(label2);

        jtf2=new JTextField();  //真实昵称
        jtf2.setBounds(90,143,110,22);
        getContentPane().add(jtf2);

        jtf = new JTextField(); //昵称
        jtf.setBounds(90, 34, 110, 22);
        getContentPane().add(jtf);

        JLabel label5 = new JLabel("密码:*");
        label5.setBounds(12, 72, 50, 17);
        getContentPane().add(label5);

        JLabel label3 = new JLabel("确认密码:*");
        label3.setBounds(12, 107, 100, 17);
        getContentPane().add(label3);

        jpf1 = new JPasswordField();//密码框
        jpf1.setBounds(90, 70, 110, 22);
        getContentPane().add(jpf1);

        jpf2 = new JPasswordField();
        jpf2.setBounds(90, 105, 110, 22);
        getContentPane().add(jpf2);

        JLabel label4 = new JLabel("性别:");
        label4.setBounds(300, 33, 40, 25);
        getContentPane().add(label4);

        sex1 = new JRadioButton("男",true);
        sex1.setBounds (350, 31,50, 30);
        getContentPane().add(sex1);
        sex0 = new JRadioButton("女");
        sex0.setBounds(400, 31, 50, 30);
        getContentPane().add(sex0);
        ButtonGroup buttonGroup = new ButtonGroup();//单选按钮组
        buttonGroup.add(sex0);
        buttonGroup.add(sex1);

        //按钮
        ok = new JButton("确定");
        ok.setBounds(50, 300, 100, 30);
        getContentPane().add(ok);

        reset = new JButton("清空");
        reset.setBounds(180, 300, 100, 30);
        getContentPane().add(reset);

        cancel = new JButton("取消");
        cancel.setBounds(320, 300, 100, 30);
        getContentPane().add(cancel);


        /****注册监听事件
         *
         */

        //取消事件的监听
        cancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent e) {
                RegisterFrame.this.dispose();   //注册窗口关闭
            }
        });
        //重置监听事件的设置
        reset.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent e) {
                jtf.setText("");
                jpf2.setText("");
                jtf2.setText("");
                jpf1.setText("");
                jtf.requestFocusInWindow();     //用户名获得焦点
            }
        });

        /***确定事件必须满足以下条件：
         * 1.用户名和密码、确定密码一定不为空
         * 2.密码和确定密码必须一致
         *
         */
        //确定事件的监听
        ok.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent e) {
                if(jtf.getText().length()==0||jpf1.getPassword().length==0||jpf2.getPassword().length==0){
                    JOptionPane.showMessageDialog(RegisterFrame.this,"带*号内容为必填选项");
                }else if (!new String(jpf1.getPassword()).equals(new String(jpf2.getPassword()))){
                    JOptionPane.showMessageDialog(RegisterFrame.this,"输入的密码不一致！");
                    jpf1.setText("");
                    jpf2.setText("");
                    jpf1.requestFocusInWindow();
                }else {//符合条件，进行用户的注册
                    //获取用户信息:
                    User user=new User();
                    //产生一个1~51号的id，并且赋予给user
                    //这里没有去重，如何解决？
                    int id= new Random().nextInt(50)+1;
                    user.setId(String.valueOf(id));
                    user.setUsername(jtf.getText());
                    user.setPwd(new String(jpf1.getPassword()));
                    //sex0:女
                    user.setSex(sex0.isSelected()?"w":"m");
                    user.setRealname(jtf2.getText());
                    boolean flag=false;
                    //进行用户注册
                    try {
                        flag=new RegisteProcess().registe(user);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }finally {
                        if(flag){
                            //弹出消息对话框
                            String content="您的用户id为:"+user.getId()+"请牢记！";
                            new CustomDialog("提示",content).setVisible(true);
                        }else {
                            //弹出错误消息对话框
                            new CustomDialog("警告","注册失败！请检查你的信息").setVisible(true);
                        }
                    }
                }
            }
        });
    }
}


/**
 * 自定义Dialog对话框
 */
class CustomDialog extends JDialog implements ActionListener {
    String title;
    String content;
    String ok = "确定";
    String cancel = "取消";

    public CustomDialog(String title, String content) {
        this.title = title;
        this.content = content;
        int width = 45, height = 45;
        // 创建1个图标实例,注意image目录要与src同级
        ImageIcon icon = new ImageIcon("images/10.png");
        icon.setImage(icon.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT));
        // 1个图片标签,显示图片
        JLabel jlImg = new JLabel(icon);
        jlImg.setSize(width, height);
        jlImg.setBounds(20, 44, width, height);
        // 1个文字标签,显示文本
        JLabel jLabel = new JLabel(content);
        jLabel.setFont(new Font("", Font.PLAIN, 14));
        // 设置文字的颜色为蓝色
        jLabel.setForeground(Color.black);
        jLabel.setBounds(75, 43, 180, 45);
        JButton okBut = new JButton(ok);
        JButton cancelBut = new JButton(cancel);
        okBut.setBackground(Color.LIGHT_GRAY);
        okBut.setBorderPainted(false);
        okBut.setBounds(65, 126, 98, 31);
        cancelBut.setBounds(175, 126, 98, 31);
        cancelBut.setBackground(Color.LIGHT_GRAY);
        cancelBut.setBorderPainted(false);
        // 给按钮添加响应事件
        okBut.addActionListener(this);
        cancelBut.addActionListener(this);
        // 向对话框中加入各组件
        add(jlImg);
        add(jLabel);
        add(okBut);
        add(cancelBut);
        // 对话框流式布局
        setLayout(null);
        // 窗口左上角的小图标
        setIconImage(icon.getImage());
        // 设置标题
        setTitle(title);
        // 设置为模态窗口,此时不能操作父窗口
        setModal(true);
        // 设置对话框大小
        setSize(300, 210);
        // 对话框局域屏幕中央
        setLocationRelativeTo(null);
        // 对话框不可缩放
        setResizable(false);
        // 点击对话框关闭按钮时,销毁对话框
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    /**
     * 当按钮被点击时会执行下面的方法
     *
     * @param e 事件
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        // 判断是不是确定按钮被点击
        if (ok.equals(e.getActionCommand())) {
            // 对话框不可见
            this.setVisible(false);
            this.dispose();
        }
        if (cancel.equals(e.getActionCommand())) {
            this.setVisible(false);
            this.dispose();
        }
    }
}
